<template>
  <view :data-theme="g.theme" class="positions-detail">
    <CustomNavbar title="持仓明细"></CustomNavbar>
    <BasicComponents></BasicComponents>
    <SearchForm
      :options="searchForm.options"
      @search-refresh="init"
      @get-form-model="form => (searchForm.form = form)"
    ></SearchForm>
    <view class="sum">汇总: {{ sumObj.profitSum }} / {{ sumObj.profitNumber }} / {{ sumObj.defectNumber }}</view>
    <view v-for="(item, index) of positionsList.positionsVo" :key="index" class="box">
      <view class="main">
        <view class="row" :class="item.itemProfit > 0 ? 'profit-success' : 'profit-error'">
          <view class="label">持仓用户</view>
          <view class="value">
            {{ item.userName }}
            <text> * {{ item.date }}</text>
            <text> * {{ item.mc }}( {{ item.dm }} )</text>
          </view>
        </view>
        <view class="row">
          <view class="label">金额 * 天数 * 盈利</view>
          <view class="value">
            {{ item.amount }}
            <text> * {{ item.days }}</text>
            <text> * {{ item.profit }}</text>
          </view>
        </view>
      </view>
      <view v-for="(son, key) in item.klist" :key="key" class="son">
        <view class="row" :class="son.profit > 0 ? 'profit-success' : 'profit-error'">
          <view class="label">持仓时间/盈利</view>
          <view class="value">{{ son.date }} * {{ son.profit }}</view>
        </view>
        <view class="row">
          <view class="label">开/收/低/高</view>
          <view class="value">{{ son.o }} * {{ son.c }} * {{ son.l }} * {{ son.h }}</view>
        </view>
      </view>
      <view class="button">
        <up-button text="卖出" :color="g.basicColor.primary" @tap="skipPage(item, 'sell')"></up-button>
        <up-button class="ml10" text="k线" :color="g.basicColor.primary_plain" @tap="skipPage(item, 'k')"></up-button>
      </view>
    </view>
    <BottomButton :is-right-btn="false"></BottomButton>
  </view>
</template>
<script setup lang="ts">
import { useSearchForm } from "./hooks/use-searchForm";
const { searchForm, getAllUser } = useSearchForm();
let positionsList = ref<any>([]);
let sumObj = ref({
  // 总盈利
  profitSum: 0,
  // 盈利数
  profitNumber: 0,
  // 亏损数
  defectNumber: 0
});
const g = useThemeStore();
onLoad(async () => {
  await getAllUser();

  nextTick(() => {
    init();
  });
});

const init = async () => {
  const params = {
    userId: searchForm.form.userId
  };

  positionsList.value = await positionsFindPositionsByList(params);
  console.log(positionsList.value, "positionsList.value");
};

const skipPage = (item, flag: string) => {
  if (flag === "k") {
    const params = setNewObjValue(item, "dm");
    uni.navigateTo({
      url: transitionParams(params, "/pages/stock/k/index")
    });
  } else {
    const params = setNewObjValue(item, "dm,quantity,userId");
    uni.navigateTo({
      url: transitionParams(params, "/pages/stock/register/index")
    });
  }
};
</script>
<style lang="scss" scoped src="./index.scss"></style>
